home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / lib / include / RCS / list.h,v < prev    next >
Encoding:
Text File  |  1991-08-16  |  10.9 KB  |  404 lines

  1. head     1.5;
  2. branch   ;
  3. access   ;
  4. symbols  sprited:1.5.1;
  5. locks    ; strict;
  6. comment  @ * @;
  7.  
  8.  
  9. 1.5
  10. date     91.02.04.09.55.36;  author ouster;  state Exp;
  11. branches 1.5.1.1;
  12. next     1.4;
  13.  
  14. 1.4
  15. date     90.09.11.14.40.07;  author kupfer;  state Exp;
  16. branches ;
  17. next     1.3;
  18.  
  19. 1.3
  20. date     89.06.23.11.29.49;  author rab;  state Exp;
  21. branches ;
  22. next     1.2;
  23.  
  24. 1.2
  25. date     89.06.15.15.33.46;  author shirriff;  state Exp;
  26. branches ;
  27. next     1.1;
  28.  
  29. 1.1
  30. date     88.06.21.09.36.53;  author ouster;  state Exp;
  31. branches ;
  32. next     ;
  33.  
  34. 1.5.1.1
  35. date     91.08.15.21.45.48;  author kupfer;  state Exp;
  36. branches ;
  37. next     ;
  38.  
  39.  
  40. desc
  41. @@
  42.  
  43.  
  44. 1.5
  45. log
  46. @Added more parens in LIST_FORALL macro.
  47. @
  48. text
  49. @/*
  50.  * list.h --
  51.  *
  52.  * Structures, macros, and routines exported by the List module.
  53.  *
  54.  * Copyright (C) 1985, 1988 Regents of the University of California
  55.  * Permission to use, copy, modify, and distribute this
  56.  * software and its documentation for any purpose and without
  57.  * fee is hereby granted, provided that the above copyright
  58.  * notice appear in all copies.  The University of California
  59.  * makes no representations about the suitability of this
  60.  * software for any purpose.  It is provided "as is" without
  61.  * express or implied warranty.
  62.  *
  63.  * rcsid "$Header: /sprite/src/lib/include/RCS/list.h,v 1.4 90/09/11 14:40:07 kupfer Exp Locker: ouster $ SPRITE (Berkeley)"
  64.  */
  65.  
  66. #ifndef _LIST
  67. #define _LIST
  68.  
  69. #ifndef _SPRITE
  70. #include "sprite.h"
  71. #endif
  72.  
  73. /*
  74.  * This module defines the list abstraction, which enables one to link
  75.  * together arbitrary data structures.  Lists are doubly-linked and
  76.  * circular.  A list contains a header followed by its real members, if
  77.  * any.  (An empty list therefore consists of a single element, the
  78.  * header,  whose nextPtr and prevPtr fields point to itself).  To refer
  79.  * to a list as a whole, the user keeps a pointer to the header; that
  80.  * header is initialized by a call to List_Init(), which creates an empty
  81.  * list given a pointer to a List_Links structure (described below).
  82.  * 
  83.  * The links are contained in a two-element structure called List_Links.
  84.  * A list joins List_Links records (that is, each List_Links structure
  85.  * points to other List_Links structures), but if the List_Links is the
  86.  * first field within a larger structure, then the larger structures are
  87.  * effectively linked together as follows:
  88.  * 
  89.  *          header
  90.  *      (List_Links)           first elt.            second elt.
  91.  *    -----------------    -----------------    ----------------- 
  92.  * ..->    |    nextPtr    | ---->    |  List_Links    | ---->    |  List_Links    |----..
  93.  *    | - - - - - - -    |    |        |    |        | 
  94.  * ..--    |    prevPtr    | <----    |        | <----    |        |<---..
  95.  *    -----------------    - ---  ---  ---    -    - ---  ---  ---    -
  96.  *                |    rest of    |    |    rest of    | 
  97.  *                |   structure    |    |   structure    | 
  98.  *                |        |    |        |
  99.  *                |      ...    |    |      ...    | 
  100.  *                -----------------    ----------------- 
  101.  * 
  102.  * It is possible to link structures through List_Links fields that are
  103.  * not at the beginning of the larger structure, but it is then necessary
  104.  * to perform pointer arithmetic to find the beginning of the larger
  105.  * structure, given a pointer to some point within it.
  106.  * 
  107.  * A typical structure might be something like:
  108.  * 
  109.  *      typedef struct {
  110.  *                  List_Links links;
  111.  *                  char ch;
  112.  *                  integer flags;
  113.  *      } EditChar;
  114.  *  
  115.  * Before an element is inserted in a list for the first time, it must
  116.  * be initialized by calling the macro List_InitElement().
  117.  */
  118.  
  119.  
  120. /*
  121.  * data structure for lists
  122.  */
  123.  
  124. typedef struct List_Links {
  125.     struct List_Links *prevPtr;
  126.     struct List_Links *nextPtr;
  127. } List_Links;
  128.  
  129. /*
  130.  * procedures
  131.  */
  132.  
  133. extern void    List_Init _ARGS_((List_Links *headerPtr)); 
  134.                 /* initialize a header to a list */
  135.  
  136. extern void    List_Insert _ARGS_((List_Links *itemPtr, List_Links *destPtr));
  137.                 /* insert an element into a list */
  138.  
  139. extern void    List_ListInsert _ARGS_((List_Links *headerPtr,
  140.                     List_Links *destPtr));
  141.                 /* insert a list into a list */
  142.  
  143. extern void    List_Remove _ARGS_((List_Links *itemPtr));
  144.                 /* remove an element from a list */
  145.  
  146. extern void    List_Move _ARGS_((List_Links *itemPtr, List_Links *destPtr));
  147.                 /* move an element elsewhere in a list */
  148.  
  149. /*
  150.  * ----------------------------------------------------------------------------
  151.  *
  152.  * List_InitElement --
  153.  *
  154.  *      Initialize a list element.  Must be called before an element is first
  155.  *    inserted into a list.
  156.  *
  157.  * ----------------------------------------------------------------------------
  158.  */
  159. #define List_InitElement(elementPtr) \
  160.     (elementPtr)->prevPtr = (List_Links *) NIL; \
  161.     (elementPtr)->nextPtr = (List_Links *) NIL;
  162.     
  163. /*
  164.  * Macros for stepping through or selecting parts of lists
  165.  */
  166.  
  167. /*
  168.  * ----------------------------------------------------------------------------
  169.  *
  170.  * LIST_FORALL --
  171.  *
  172.  *      Macro to loop through a list and perform an operation on each member.
  173.  *
  174.  *      Usage: LIST_FORALL(headerPtr, itemPtr) {
  175.  *                 / * 
  176.  *                   * operation on itemPtr, which points to successive members
  177.  *                   * of the list
  178.  *                   * 
  179.  *                   * It may be appropriate to first assign
  180.  *                   *          foobarPtr = (Foobar *) itemPtr;
  181.  *                   * to refer to the entire Foobar structure.
  182.  *                   * /
  183.  *             }
  184.  *
  185.  *      Note: itemPtr must be a List_Links pointer variable, and headerPtr
  186.  *      must evaluate to a pointer to a List_Links structure.
  187.  *
  188.  * ----------------------------------------------------------------------------
  189.  */
  190.  
  191. #define LIST_FORALL(headerPtr, itemPtr) \
  192.         for ((itemPtr) = List_First(headerPtr); \
  193.              !List_IsAtEnd((headerPtr),(itemPtr)); \
  194.              (itemPtr) = List_Next(itemPtr))
  195.  
  196. /*
  197.  * ----------------------------------------------------------------------------
  198.  *
  199.  * List_IsEmpty --
  200.  *
  201.  *      Macro: Boolean value, TRUE if the given list does not contain any
  202.  *      members.
  203.  *
  204.  *      Usage: if (List_IsEmpty(headerPtr)) ...
  205.  *
  206.  * ----------------------------------------------------------------------------
  207.  */
  208.  
  209. #define List_IsEmpty(headerPtr) \
  210.         ((headerPtr) == (headerPtr)->nextPtr)
  211.  
  212. /*
  213.  * ----------------------------------------------------------------------------
  214.  *
  215.  * List_IsAtEnd --
  216.  *
  217.  *      Macro: Boolean value, TRUE if itemPtr is after the end of headerPtr
  218.  *      (i.e., itemPtr is the header of the list).
  219.  *
  220.  *      Usage: if (List_IsAtEnd(headerPtr, itemPtr)) ...
  221.  *
  222.  * ----------------------------------------------------------------------------
  223.  */
  224.  
  225.  
  226. #define List_IsAtEnd(headerPtr, itemPtr) \
  227.         ((itemPtr) == (headerPtr))
  228.  
  229.  
  230. /*
  231.  * ----------------------------------------------------------------------------
  232.  *
  233.  * List_First --
  234.  *
  235.  *      Macro to return the first member in a list, which is the header if
  236.  *      the list is empty.
  237.  *
  238.  *      Usage: firstPtr = List_First(headerPtr);
  239.  *
  240.  * ----------------------------------------------------------------------------
  241.  */
  242.  
  243. #define List_First(headerPtr) ((headerPtr)->nextPtr)
  244.  
  245. /*
  246.  * ----------------------------------------------------------------------------
  247.  *
  248.  * List_Last --
  249.  *
  250.  *      Macro to return the last member in a list, which is the header if
  251.  *      the list is empty.
  252.  *
  253.  *      Usage: lastPtr = List_Last(headerPtr);
  254.  *
  255.  * ----------------------------------------------------------------------------
  256.  */
  257.  
  258. #define List_Last(headerPtr) ((headerPtr)->prevPtr)
  259.  
  260. /*
  261.  * ----------------------------------------------------------------------------
  262.  *
  263.  * List_Prev --
  264.  *
  265.  *      Macro to return the member preceding the given member in its list.
  266.  *      If the given list member is the first element in the list, List_Prev
  267.  *      returns the list header.
  268.  *
  269.  *      Usage: prevPtr = List_Prev(itemPtr);
  270.  *
  271.  * ----------------------------------------------------------------------------
  272.  */
  273.  
  274. #define List_Prev(itemPtr) ((itemPtr)->prevPtr)
  275.  
  276. /*
  277.  * ----------------------------------------------------------------------------
  278.  *
  279.  * List_Next --
  280.  *
  281.  *      Macro to return the member following the given member in its list.
  282.  *      If the given list member is the last element in the list, List_Next
  283.  *      returns the list header.
  284.  *
  285.  *      Usage: nextPtr = List_Next(itemPtr);
  286.  *
  287.  * ----------------------------------------------------------------------------
  288.  */
  289.  
  290. #define List_Next(itemPtr) ((itemPtr)->nextPtr)
  291.  
  292.  
  293. /*
  294.  * ----------------------------------------------------------------------------
  295.  *      The List_Insert procedure takes two arguments.  The first argument
  296.  *      is a pointer to the structure to be inserted into a list, and
  297.  *      the second argument is a pointer to the list member after which
  298.  *      the new element is to be inserted.  Macros are used to determine
  299.  *      which existing member will precede the new one.
  300.  *
  301.  *      The List_Move procedure takes a destination argument with the same
  302.  *      semantics as List_Insert.
  303.  *
  304.  *      The following macros define where to insert the new element
  305.  *      in the list:
  306.  *
  307.  *      LIST_AFTER(itemPtr)     --      insert after itemPtr
  308.  *      LIST_BEFORE(itemPtr)    --      insert before itemPtr
  309.  *      LIST_ATFRONT(headerPtr) --      insert at front of list
  310.  *      LIST_ATREAR(headerPtr)  --      insert at end of list
  311.  *
  312.  *      For example, 
  313.  *
  314.  *              List_Insert(itemPtr, LIST_AFTER(otherPtr));
  315.  *
  316.  *      will insert itemPtr following otherPtr in the list containing otherPtr.
  317.  * ----------------------------------------------------------------------------
  318.  */
  319.  
  320. #define LIST_AFTER(itemPtr) ((List_Links *) itemPtr)
  321.  
  322. #define LIST_BEFORE(itemPtr) (((List_Links *) itemPtr)->prevPtr)
  323.  
  324. #define LIST_ATFRONT(headerPtr) ((List_Links *) headerPtr)
  325.  
  326. #define LIST_ATREAR(headerPtr) (((List_Links *) headerPtr)->prevPtr)
  327.  
  328. #endif /* _LIST */
  329. @
  330.  
  331.  
  332. 1.5.1.1
  333. log
  334. @Initial branch for Sprite server.
  335. @
  336. text
  337. @d15 1
  338. a15 1
  339.  * rcsid "$Header: /sprite/src/lib/include/RCS/list.h,v 1.5 91/02/04 09:55:36 ouster Exp $ SPRITE (Berkeley)"
  340. @
  341.  
  342.  
  343. 1.4
  344. log
  345. @Use function prototypes.
  346. @
  347. text
  348. @d15 1
  349. a15 1
  350.  * rcsid "$Header: /sprite/src/lib/include/RCS/list.h,v 1.3 89/06/23 11:29:49 rab Exp Locker: kupfer $ SPRITE (Berkeley)"
  351. d144 3
  352. a146 3
  353.         for (itemPtr = List_First(headerPtr); \
  354.              !List_IsAtEnd((headerPtr),itemPtr); \
  355.              itemPtr = List_Next(itemPtr))
  356. @
  357.  
  358.  
  359. 1.3
  360. log
  361. @*** empty log message ***
  362. @
  363. text
  364. @d15 1
  365. a15 1
  366.  * rcsid "$Header: /sprite/src/lib/include/RCS/list.h,v 1.2 89/06/15 15:33:46 shirriff Exp Locker: rab $ SPRITE (Berkeley)"
  367. d85 15
  368. a99 5
  369. void    List_Init();    /* initialize a header to a list */
  370. void    List_Insert();  /* insert an element into a list */
  371. void    List_ListInsert();  /* insert a list into a list */
  372. void     List_Remove();  /* remove an element from a list */
  373. void     List_Move();    /* move an element elsewhere in a list */
  374. @
  375.  
  376.  
  377. 1.2
  378. log
  379. @Added List_ListInsert declaration.
  380. @
  381. text
  382. @d15 1
  383. a15 1
  384.  * rcsid "$Header: /sprite/src/lib/include/RCS/list.h,v 1.1 88/06/21 09:36:53 ouster Exp Locker: shirriff $ SPRITE (Berkeley)"
  385. d23 1
  386. a23 1
  387. #endif _SPRITE
  388. d270 1
  389. a270 1
  390. #endif _LIST
  391. @
  392.  
  393.  
  394. 1.1
  395. log
  396. @Initial revision
  397. @
  398. text
  399. @d15 1
  400. a15 1
  401.  * rcsid "$Header: list.h,v 1.1 88/06/20 09:27:29 ouster Exp $ SPRITE (Berkeley)"
  402. d87 1
  403. @
  404.